<template>
  <view class="page container">
    <u-form
      :model="basicForm"
      :rules="rules"
      labelPosition="top"
      ref="form"
      labelWidth="auto"
      :labelStyle="{
      	fontSize: '14px',
      	color: '#B2B2B2',
      	lineHight: '20px'
      }"
    >
      <view class="basic-style">
        <u-form-item label="业务名称" prop="taskId" required>
					<selectProjectTaskInputPopup
					  v-model="basicForm.taskId"
						:projectId="projectId"
						:isRequest="!!projectId"
						:redistributionDtList="redistributionDtList"
					  style="width: 100%;"
					  placeholder="请选择业务名称"
					  border="surround"
					  suffixIcon="arrow-down-fill"
					  :suffixIconStyle="{ color: '#B2B2B2' }"
						@change="changeTaskId"
					></selectProjectTaskInputPopup>
        </u-form-item>
				<u-form-item label="生产团队" prop="productTeam" required>
					<h-select-team-input
					  disabled
					  style="width: 100%"
					  v-model="basicForm.productTeam"
					  objectField="basicForm.productTeam"
					  placeholder="请选择生产团队"
					  border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						:triggerChange="true"
					  :tdlx="2"
					/>
				</u-form-item>
				<u-form-item label="款项分配占比(%)" prop="paymentRate" required>
					<h-number-input
						placeholder="请输入款项分配占比"
						v-model="basicForm.paymentRate"
						:digit="2"
						:min="0.01"
						:max="100"
						@blur="paymentRateBlur"
					></h-number-input>
				</u-form-item>
        <u-form-item label="款项分配金额(元)" prop="paymentMoney" required>
					<h-number-input
						disabled
						placeholder="请输入款项分配金额"
						v-model="basicForm.paymentMoney"
						:digit="2"
						:min="0"
					></h-number-input>
        </u-form-item>
        <u-form-item label="任务说明" prop="taskDesc">
          <u--input disabled placeholder="请输入任务说明" v-model="basicForm.taskDesc"></u--input>
        </u-form-item>
      </view>
      <view class="space-x-1 mt-1" style="display: flex">
        <u-button type="primary" text="保存" :loading="loading" @click="onSubmit"></u-button>
        <u-button type="primary" text="关闭" :loading="loading" @click="onClose"></u-button>
      </view>
    </u-form>
  </view>
</template>

<script>
import selectProjectTaskInputPopup from './reference/selectProjectTaskInputPopup'
export default {
  name: 'planTaskForm',
	components: {
		selectProjectTaskInputPopup
	},
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    datailDisabled: {
      type: Boolean,
      default: false,
    },
    index: {
      type: Number,
      default: null,
    },
		// 收付款计划金额
		totalPrice: {
      type: [Number, String],
      default: null,
    },
		// 项目id
		projectId: {
			type: [Number, String],
			default: null,
		},
		// 收付款计划id
		planId: {
			type: [Number, String],
			default: null,
		},
		// 应收状态
		shouldPayFlag: {
			type: [String, Number],
			default: ''
		},
		// 分配任务信息列表
		redistributionDtList: {
			type: [Array],
			default: () => []
		}
  },
  data() {
    return {
      loading: false,
			teamId: '',
			companyId: '',
      basicForm: {
        taskId: '',
        productTeam: '',
        paymentRate: '',
        paymentMoney: '',
				taskDesc: ''
      },
      rules: {
        taskId: [{ required: true, message: '请选择业务名称!' }],
        productTeam: [{ required: true, message: '请选择生产团队!' }],
        paymentRate: [{ required: true, message: '请输入款项分配占比!' }],
        paymentMoney: [{ required: true, message: '请输入款项分配金额!' }],
      },
    }
  },
  created() {},
  methods: {
    reset() {
      this.$refs.form.resetFields()
    },
		// 计算款项分配金额
		paymentRateBlur (val) {
			this.$nextTick(() => {
				if (this.basicForm.paymentRate === null || this.basicForm.paymentRate === undefined || this.basicForm.paymentRate === '') {
					this.$set(this.basicForm, 'paymentMoney', '')
				} else {
					let totalPrice = this.totalPrice || 0
					let paymentRate = this.basicForm.paymentRate
					let amount = totalPrice * paymentRate / 100
					let paymentMoney = Math.round((amount + Number.EPSILON) * 100) / 100;
					this.$set(this.basicForm, 'paymentMoney', paymentMoney)
				}
			})
		},
		changeTaskId (val) {
		this.$set(this.basicForm, 'productTeam', val.productTeam); // 生产团队
		this.$set(this.basicForm, 'taskDesc', val.taskDescription); // 任务说明
		this.$set(this.basicForm, 'categoryName', val.categoryName); // 业务名称 
		this.$set(this.basicForm, 'productTeamName', val.productTeam_dictText); // 业务名称
	},
    async onSubmit() {
      await this.$refs.form.validate()
			if(this.loading) return
			this.loading = true
			this.basicForm.projectId = this.projectId
			this.basicForm.planId = this.planId
      if (!this.index) {
        this.$emit('submit', [{
          // id: parseInt(Math.random() * 1000000000000),
          ...this.basicForm,
        }, this.index])
      } else {
        this.$emit('submit', [this.basicForm, this.index])
      }
    },
    onClose() {
			this.loading = false
      this.$emit('close')
    }
  }
}
</script>

<style scoped></style>
